<?php


function list_tatib(){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Nomor Surat'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Tanggal Surat'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Jumlah Karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	if(isset($_GET['key']))
	$query = "SELECT * FROM tatib x 
	INNER JOIN tatib_detail a ON a.tatib_id = x.tatib_id
INNER JOIN karyawan c ON a.karyawan_id = c.karyawan_id 
INNER JOIN biodata d ON d.biodata_id = c.biodata_id
WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )";
	else
	$query = "SELECT * FROM tatib  ";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=tatib&task=edit&id='.$ey['tatib_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
						'href'=>'index.php?com=tatib&task=detail&jid='.$ey['tatib_id'],
						'title'=>'Edit'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['tatib_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'=>  strtoupper($ey['nomor'] ),   
			'tanggal'=>  strtoupper($ey['tanggal'] ),  
			'jumlah'=>  position_text_align( get_jumlah_karyawan_sp($ey['tatib_id']),'center') ,
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	if(is_admin()):
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com=tatib&task=edit&id=0\' });'),
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=tatib\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
	else:
	
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=tatib\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
	endif;	
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop , $search);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu .table_builder($header , $datas ,  5 , false,$paging );
} 

function get_jumlah_karyawan_sp($tatib_Id){
	$query = "SELECT * FROM tatib_detail WHERE tatib_id ={$tatib_Id}";
	$result = my_query($query);
	return my_num_rows($result);
}

function form_tatib( $id ){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('tatib','tatib_id',$id);
	
	$nomor = array(
			'name'=>'nomor',
			'value'=>(isset($_POST['nomor'])? $_POST['nomor'] : $fields['nomor']),
			'id'=>'nomor',
			'type'=>'textfield',
			'size'=>'35'
		);
	$form_nomor = form_dynamic($nomor);
	$view .= form_field_display( $form_nomor  , "Nomor SK"  );
	$ftanggalm = $ftanggal = date('d-m-Y');
	if($fields){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$fields['tanggal'] );
		list($yyyytanggalm , $mmtanggalm,$ddtanggalm ) = explode("-" ,$fields['tanggal_mulai'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal;
		$ftanggalm = $ddtanggalm.'-'.$mmtanggalm.'-'.$yyyytanggalm;
	}
	
	$tanggal = array(
			'name'=>'tanggal',
			'value'=>(isset($_POST['tanggal'])? $_POST['tanggal'] : $ftanggal),
			'id'=>'tanggal',
			'type'=>'textfield',
			'size'=>'45'
		);
	$form_tanggal = form_calendar($tanggal);
	$view .= form_field_display( $form_tanggal  , "Tanggal" );
	
	 
	
	$tanggal_mulai = array(
			'name'=>'tanggal_mulai',
			'value'=>(isset($_POST['tanggal_mulai'])? $_POST['tanggal_mulai'] : $ftanggalm),
			'id'=>'tanggal_mulai' 
		);
	$form_tanggal_mulai = form_calendar($tanggal_mulai);
	//$view .= form_field_display( $form_tanggal_mulai , "Tanggal mulai" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function submit_tatib($id = 0){

	if($_POST){
		list( $ddtanggal, $mmtanggal,$yyyytanggal ) = explode("-" ,$_POST['tanggal'] );
		$ftanggal = $yyyytanggal.'-'.$mmtanggal.'-'.$ddtanggal;
		 
	}
	$datas = array(
		'nomor'=>my_type_data_str($_POST['nomor']),
		'tanggal'=>my_type_data_str($ftanggal), 
	);
	 
	if($id > 0){
	
		return my_update_record('tatib','tatib_id',$id,$datas);
	}
	$new_id = my_insert_record('tatib',$datas);
	return $new_id;
}

function detail_tatib($tatib_id){
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'Jenis Peringatan'=>array('style'=>'border-bottom:2px solid;width:15%'),   
		'Uraian'=>array('style'=>'border-bottom:2px solid;width:20%'),   
		'Sangsi'=>array('style'=>'border-bottom:2px solid;width:15%'),   
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM tatib_detail a 
				INNER JOIN tatib  b ON a.tatib_id = b.tatib_id
				INNER JOIN jenis_peringatan c on c.jenis_peringatan_id = a.jenis_peringatan_id  				
				WHERE a.tatib_id = {$tatib_id}";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=tatib&task=edit_detail&sid='.$ey['tatib_detail_id'].'&id='.$tatib_id,
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['tatib_detail_id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 

		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>  $karyawan['nik'] ,
			'nama'=>  $karyawan['nama_gelar'] , 
			'ke'=> $ey['nama_jenis_peringatan']  ,   
			'uraian'=> $ey['uraian']  ,   
			'sangsi'=> $ey['sangsi']  ,   
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	$tatib = my_get_data_by_id('tatib','tatib_id',$_GET['jid']);
	$view ='<table width="98%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "detail pegawai" , "cp"  );
	$view .= '<tr><td><td><div style="width:100%;text-align:right;">
		<input type="button" value="Tambah data" onclick="javascript: jQuery.facebox({ ajax: \'index.php?com=tatib&task=edit_detail&sid=0&id='.$tatib_id.'\' });" class="main_button" /> 
		<!-- input type="button" value="Cetak" class="main_button" onclick="javascript:window.open(\'index.php?com=report&task=personal_resume&sid=0&id='.$id.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');"/ --> 
		<input type="button" value="Back" class="main_button" onclick="javascript:location.href=\'index.php?com=tatib\'"/></div></td></tr>'   ; 
	$view .= form_field_display( '<span class="label_form">'.  $tatib['nomor'] .'</span>'   , "<b>Nomor Surat</b>"    ); 
	$view .= form_field_display( '<span class="label_form">'.    date( "d-m-Y" , strtotime($tatib['tanggal'].' 00:00:00')).'</span>'   , "<b>Tanggal Surat</b>"    ); 
	//$view .= form_field_display( '<span class="label_form">'.    date( "d-m-Y" , strtotime($seleksi['tanggal_mulai'].' 00:00:00')) .'</span>'   , "<b>Tanggal berjalan (TMT)</b>"    ); 
	$view .= form_footer( );
	$view .= '</td></tr></table><br/>';
	$datas = table_rows($row);   
	return  $view . table_builder($header , $datas , 7, false );
}

function form_tatib_detail($sid , $id){
	
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');
	$view = form_header( "seleski" , "seleski"  );
 	$fields = my_get_data_by_id('tatib_detail','tatib_detail_id',$sid);
	$sinternal = my_get_data_by_id('tatib','tatib_id',$id);
	$view .= form_field_display( $sinternal['nomor']  , "Nomor Surat"  );

	$ftanggalm = $ftanggal = '';
	if($sinternal){
		list($yyyytanggal , $mmtanggal,$ddtanggal ) = explode("-" ,$sinternal['tanggal'] );
		$ftanggal = $ddtanggal.'-'.$mmtanggal.'-'.$yyyytanggal;
	} 
	$view .= form_field_display( $ftanggal  , "Tanggal Surat" ); 
	
	$karyawan_data = loaddata_karyawan(( $fields ? $fields['karyawan_id'] : 0));
	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>(isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : $karyawan_data['nik'].'/'.$karyawan_data['nama']  ),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'50'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan  , "NIK/Nama Karyawan"  );

	$datas = array();
	 $query_golongan = "SELECT * FROM jenis_peringatan ORDER BY nomor_urut";
	 $re = my_query($query_golongan);
	 while($row = my_fetch_array($re) ){
		$datas[$row['jenis_peringatan_id']] =$row['nama_jenis_peringatan'];
	 }
	$temp = array(
		'name'=>'jenis_peringatan_id',
		'id'=>'jenis_peringatan_id',
		'value'=>isset($_POST['jenis_peringatan_id']) ? $_POST['jenis_peringatan_id'] : $fields['jenis_peringatan_id'],
	);
	$form_temp = form_dropdown($temp , $datas);
	$view .= form_field_display( $form_temp , "Jenis Peringatan" );
	
	$uraian  = array(
		'name'=>'uraian',
		'value'=>(isset($_POST['uraian'])? $_POST['uraian'] : $fields['uraian']  ),
		'id'=>'uraian'  
	);
	$form_uraian = form_textarea($uraian);
	$view .= form_field_display( $form_uraian  , "Uraian"  );
	
	$sangsi  = array(
		'name'=>'sangsi',
		'value'=>(isset($_POST['sangsi'])? $_POST['sangsi'] : $fields['sangsi']  ),
		'id'=>'sangsi' ,
		'type'=>'textfield','size'=>'45'
	);
	$form_sangsi = form_dynamic($sangsi);
	$view .= form_field_display( $form_sangsi  , "Sangsi"  );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function submit_tatib_detail($id , $sid){
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	$datas = array(
		'tatib_id'	=> my_type_data_int($id),
		'karyawan_id'	=> my_type_data_int($karyawan_id),
		'jenis_peringatan_id'	=> my_type_data_str($_POST['jenis_peringatan_id'] ),
		'uraian'	=> my_type_data_str($_POST['uraian'] ),
		'sangsi'	=> my_type_data_str($_POST['sangsi'] ),
		'datetime_added'	=> my_type_data_function(' NOW() '),
	);
	if($sid > 0){
		return my_update_record('tatib_detail' ,'tatib_detail_id' ,$sid, $datas);
	}
	return my_insert_record('tatib_detail' , $datas);
}

